<?php
namespace Src;

class Redis
{
    //protected static $_instance = null;
    protected $redis;
    protected $hostname;
    protected $port;

    public function __construct()
    {
        $redis = Config::getConfig()['redis'];//读取配置信息
        $this->hostname = $redis['hostname'];
        $this->port = $redis['port'];
        $this->redis = new \redis();
        $this->redis->connect($this->hostname, $this->port);
        //self::$redis->auth($db['password']);
    }

    private function __clone() {}

    //为了兼容协程，这里不同单例模式
//    public static function getInstance()
//    {
//        if (self::$_instance === null) {
//            self::$_instance = new self();
//        }
//        return self::$_instance;
//    }

    public function set($key, $value)
    {
        $this->redis->set($key, $value);
    }

    public function get($key)
    {
        return $this->redis->get($key);
    }

    public function __destruct()
    {
        $this->redis = null;
        //echo "Redis finished".PHP_EOL;
    }

}